import prompt from '@system.prompt';
import router from '@system.router';
import http from '@ohos.net.http';

export default {
    data: {
        title: 'World',
        user: {
            "userId": null, "userName": null, "userPassword": null,
            "userQuestion": null, "userAnswer": null, "userImagePath": null
        },
        "array": [
            {"value": "我的订单"},
            {"value": "问题反馈"},
            {"value": "更改头像"},
            {"value": "更改用户名"},
            {"value": "重置密码"},
        ],
        imagePath:"",
        newUserName:""

    },

    click(){
        this.imagePath=this.$app.$def.globalData.user.userImagePath;
    },

    setImagePath(path){
        this.imagePath=path;
    },
    onInit(){
        this.imagePath=this.$app.$def.globalData.user.userImagePath;
        this.newUserName=this.$app.$def.globalData.user.userName;
        console.log("-------mine-----");
    },

    showeventDialog(e) {
        this.$element('eventDialog').show()
    },
    getList(e) {
        this.newUserName = e.value
    },
    setList(e) {
        if(this.newUserName){
            prompt.showToast({
                message: '已提交'
            })
            this.$element('eventDialog').close()
            this.resetUserNameRequest(this.newUserName);
        }else{
            this.$element('eventDialog').close()
        }
    },
    onInit1(){
        console.log("==========="+this.imagePath);
    },

    resetImageRequest(){
        let httpRequest = http.createHttp();
        httpRequest.request(
        // 填写http请求的url地址，可以带参数也可以不带参数。URL地址需要开发者自定义。GET请求的参数可以在extraData中指定
        this.$app.$def.getUserUpdateImageUrl(),
            {
                method: 'POST',
                header: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                },
                extraData: {
                    userName: this.$app.$def.globalData.user.userName,
                    userPassword: this.$app.$def.globalData.user.userPassword
                }
            }, (err, data) => {
            if (!err) {
                // data.result为http响应内容，可根据业务需要进行解析
                console.info('Result:' + data.result);
                console.log("--------->")
                this.$app.$def.globalData.jsonResult = JSON.parse(data.result);
                if(this.$app.$def.globalData.jsonResult.code == 1){
                    this.$app.$def.globalData.user.userImagePath=this.$app.$def.globalData.jsonResult.info;
                    this.setImagePath(this.$app.$def.globalData.jsonResult.info);
                    this.imagePath = this.$app.$def.globalData.jsonResult.info;
                    console.log("mine.image=>" + this.imagePath);
                }
                else{
                    this.msg = this.$app.$def.globalData.jsonResult.msg
                    prompt.showToast({
                        message: this.msg,
                        duration: 2000,
                    });

                }
                // data.header为http响应头，可根据业务需要进行解析
                console.info('header:' + data.header);
            } else {
                console.info('error:' + err.data);
            }
        }
        );
    },
    resetUserNameRequest(newName){
        let httpRequest = http.createHttp();
        httpRequest.request(
        // 填写http请求的url地址，可以带参数也可以不带参数。URL地址需要开发者自定义。GET请求的参数可以在extraData中指定
        this.$app.$def.getUserUpdateNameUrl(),
            {
                method: 'POST',
                header: {
                    'Content-Type': 'application/json',
                },
                extraData: {
                    userName: this.$app.$def.globalData.user.userName,
                    userPassword: this.$app.$def.globalData.user.userPassword,
                    goalUserName:newName,
                }
            }, (err, data) => {
            if (!err) {
                // data.result为http响应内容，可根据业务需要进行解析
                console.info('Result:' + data.result);
                console.log("--------->")
                this.$app.$def.globalData.jsonResult = JSON.parse(data.result);
                if(this.$app.$def.globalData.jsonResult.code == 1){
                    this.$app.$def.globalData.user.userName=this.$app.$def.globalData.jsonResult.info;
                }
                else{
                    this.msg = this.$app.$def.globalData.jsonResult.msg
                    prompt.showToast({
                        message: this.msg,
                        duration: 2000,
                    });

                }
                // data.header为http响应头，可根据业务需要进行解析
                console.info('header:' + data.header);
            } else {
                console.info('error:' + err.data);
            }
        }
        );
    },
    jump(id){
        if (id==0) {
            router.push({
                uri: "pages/myorder/myorder"
            })

        }else if (id==1) {
            router.push({
                uri: "pages/problemfeedback/problemfeedback"
            })
        }
        else if (id==2) {
            this.resetImageRequest();
        }
        else if (id==3) {
            this.showeventDialog();
        }
        else if (id==4) {
            router.push({
                uri: "pages/resetpassword/resetpassword"
            })
        }
    }

}
